package com.zhixin.fans.ks.ui.main;

import android.support.v4.view.ViewPager;

import com.blankj.utilcode.util.SizeUtils;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.zhixin.fans.ks.CoupleApp;
import com.zhixin.fans.ks.R;
import com.zhixin.fans.ks.api.CommonApi;
import com.zhixin.fans.ks.api.res.FirstChargeRes;
import com.zhixin.fans.ks.api.res.UserInfoRes;
import com.zhixin.fans.ks.manager.UserInfoManager;
import com.zhixin.fans.ks.ui.dialog.FirstChargeHintDialog;
import com.zhixin.fans.ks.ui.main.index.IndexV2Fragment;
import com.zhixin.fans.ks.ui.main.person.PersonalFragment;
import com.zhixin.fans.ks.ui.main.service.RankFragment;

import org.eenie.common.base.BaseFragment;
import org.eenie.common.base.BaseFragmentActivity;
import org.eenie.common.base.WinMode;
import org.eenie.common.subscribes.ProgressSubscriber;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseFragmentActivity implements QMUITabSegment.OnTabClickListener, ViewPager.OnPageChangeListener {
    public static final String ROUTE_PATH = "/main/main_home";

    List<BaseFragment> mFragments = new ArrayList<>();
    @BindView(R.id.page_fragment_v2)
    ViewPager mPageFragment;
    @BindView(R.id.taber)
    QMUITabSegment mTaber;


    MainPageFragmentAdapter mMainPageFragmentAdapter;

//    CoupleApi mCoupleApi;
    UserInfoManager mUserInfoManager;

    CommonApi mCommonApi;


    @Override
    public int initContentView() {
        return R.layout.activity_main_plugin;
    }

    @Override
    public void initInjector() {
        setWinMode(WinMode.IMMERSE);
        mCommonApi = ((CoupleApp) getApplication()).getApplicationComponent().getApiManager().create(CommonApi.class);
        mUserInfoManager = ((CoupleApp) getApplication()).getCoupleAppComponent().getUserInfoManager();
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mFragments.add(new IndexV2Fragment("首页"));
        mFragments.add(new RankFragment("客服"));
        mFragments.add(new PersonalFragment("我的"));


        mPageFragment.setOffscreenPageLimit(mFragments.size());
        mPageFragment.setAdapter(mMainPageFragmentAdapter = new MainPageFragmentAdapter(getSupportFragmentManager(), mFragments));

        mTaber.setDefaultSelectedColor(getResources().getColor(R.color.colorPrimary));

        mTaber.addTab(new QMUITabSegment.Tab(getResources().getDrawable(R.mipmap.ic_main_bottom_index_nomal), getResources().getDrawable(R.mipmap.ic_main_bottom_index_selected), mFragments.get(0).getTitle(), false));
        mTaber.addTab(new QMUITabSegment.Tab(getResources().getDrawable(R.mipmap.ic_main_bottom_service_normal), getResources().getDrawable(R.mipmap.ic_main_bottom_service_selected), mFragments.get(1).getTitle(), false));
        mTaber.addTab(new QMUITabSegment.Tab(getResources().getDrawable(R.mipmap.ic_main_bottom_me_normal), getResources().getDrawable(R.mipmap.ic_main_bottom_me_selected), mFragments.get(2).getTitle(), false));

        mTaber.setDefaultTabIconPosition(QMUITabSegment.ICON_POSITION_TOP);
        mTaber.setTabTextSize(SizeUtils.sp2px(10));
        mTaber.notifyDataChanged();
        mTaber.selectTab(0);


        mPageFragment.addOnPageChangeListener(this);
        mTaber.setOnTabClickListener(this);

        fetchFirst();
    }


    @Override
    public void onBackPressedSupport() {
        new QMUIDialog.MessageDialogBuilder(mContext)
                .setMessage("确定退出应用")
                .setTitle("提示")
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        finish();
                    }
                })
                .create()
                .show();
    }


    @Override
    public void onTabClick(int index) {
        mPageFragment.setCurrentItem(index, true);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mTaber.selectTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    @Override
    protected void onResume() {
        super.onResume();
        if (mUserInfoManager.isLogin()) {
            fetchUserInfo();
        }
    }

    private void fetchUserInfo() {
        mCommonApi.fetchUserInfo("")
                .subscribe(new ProgressSubscriber<UserInfoRes>() {
                    @Override
                    public void onNext(UserInfoRes userInfoDetail) {
                        mUserInfoManager.save(userInfoDetail.getUserInfo());
                        mUserInfoManager.setValue(userInfoDetail.getUserInfo());
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }


    private void fetchFirst() {
        if (mUserInfoManager.isLogin()) {
            mCommonApi.fetchFirstChangePay()
                    .delay(1, TimeUnit.SECONDS)
                    .subscribe(new ProgressSubscriber<FirstChargeRes>() {
                        @Override
                        public void onNext(FirstChargeRes firstChargeRes) {
                            if (firstChargeRes.getFirstFlag() == 1) {
                                new FirstChargeHintDialog()
                                        .show(getSupportFragmentManager(), "first");
                            }
                        }

                        @Override
                        public void onFinish() {

                        }
                    });
        }
    }

}
